//
//  ActivityDetailViewController.m
//  豆瓣
//
//  Created by dllo on 15/12/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ActivityDetailViewController.h"
#import "ActivityDetailView.h"
#import "DataBaseSingleton.h"
#import "LoginViewController.h"

@interface ActivityDetailViewController ()

@property (nonatomic, retain) ActivityDetailView *activityDV;

@end

@implementation ActivityDetailViewController

- (void)dealloc{
    [_activityDV release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    
    self.navigationItem.title = self.activityList.title;
    self.activityDV = [[ActivityDetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.activityDV.activity = self.activityList;
    [self.view addSubview:_activityDV];
    [_activityDV release];
    
    
    UIImage *rightImage = [[UIImage imageNamed:@"btn_nav_share"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    
    
    UIImage *leftImage = [[UIImage imageNamed:@"btn_nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
}

- (void)leftBarButtonAction:(UIBarButtonItem *)button{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)rightBarButtonAction:(UIBarButtonItem *)button{
    
    // 判断登陆状态
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"];
    if ([string isEqualToString:@"yes"]) {
        
        NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectActivity];
        NSMutableArray *nameArray = [NSMutableArray array];
        for (ActivityList *act in array) {
            [nameArray addObject:act.title];
        }
        
        if ([nameArray containsObject:self.activityList.title]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *acAc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [alertC addAction:acAc];
            
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
            
        }else {
            // 插入收藏的数据
            [[DataBaseSingleton shareDataBase] insertActivityIntoTable:self.activityList];
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *acAc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [alertC addAction:acAc];
            
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
        }
        
    }else if ([string isEqualToString:@"no"]){
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
